<?php
class AdminController extends MyController
{
	public function init()
	{
		parent::init();
	}

	public function actionIndex()
	{
		$page = intval(Yii::app()->request->getParam('pageNum')) - 1;

		// 查库
		$CDbCriteria = new CDbCriteria;
		$CDbCriteria->order = "id desc";

		// 分页
		$count = Admin::model()->count($CDbCriteria);
		$pages = new CPagination($count);
		$pages->pageSize = self::PAGE_SIZE;
		$pages->currentPage = $page;
		$pages->applyLimit($CDbCriteria);

		$rows = Admin::model()->findAll($CDbCriteria);

		// 显示
		$data = array();
		$data['rows'] = $rows;
		$data['pages'] = $pages;
		$this->display('index', $data);
	}
	public function actionAdd()
	{
		if(Yii::app()->request->getParam('action') == 'add_user')
		{
			$username = trim(Yii::app()->request->getParam('username'));
			$password = trim(Yii::app()->request->getParam('password'));
			$role = intval(Yii::app()->request->getParam('role'));
			if(Admin::model()->find('username=:name',array(':name'=>$username)))
			{
				$this->alert_error('用户名已经存在');
			}
			if(!$username || !$password)
			{
				$this->alert_error('请将表单项填写完整');
			}
			$user = new Admin();
			$user->username = $username;
			$user->password = self::hash_password($password);
			$user->role = isset(Roles::$roles[$role]) ? $role : 2;
			if($user->save())
			{
				$this->alert_ok();
			}
			else
			{
				$this->alert_error('内部错误，请联系网站技术人员');
			}
		}
		$this->display('add');
	}
	public function actionEdit()
	{
		$id = intval(Yii::app()->request->getParam('id'));
		if(!$id || !$user = Admin::model()->findByPk($id))
		{
			$this->alert_error('此用户不存在');
		}
		if(Yii::app()->request->getParam('action') == 'edit_user')
		{
			$password = trim(Yii::app()->request->getParam('password'));
			$role = intval(Yii::app()->request->getParam('role'));
			if($password)
			{
				$user->password = self::hash_password($password);
			}
			$user->role = $role;
			if($user->save())
			{
				$this->alert_ok();
			}
			else
			{
				$this->alert_error('内部错误，请联系网站技术人员');
			}
		}
		$this->display('edit',array('user'=>$user));
	}
	public function actionDelete()
	{
		$id = intval(Yii::app()->request->getParam('id'));
		if(!$id)
		{
			$this->alert_error('ID不能为空');
		}

		//是否存在
		if(!$admin = Admin::model()->findByPk($id))
		{
			$this->alert_error('此用户不存在');
		}
		
		if($admin->role == 1)
		{
			$this->alert_error('超级管理员不能被删除，请先修改其权限');
		}
		if($admin->delete())
		{
			$this->alert_ok('删除成功');
		}
		else 
		{
			$this->alert_error('删除失败，请联系技术员');
		}
	}
	
	public function actionEditmypwd()
	{
		if(Yii::app()->request->getParam('edit'))
		{
			$admin = Admin::model()->findByPk($this->_userinfo()->id);
			$oldpwd = Yii::app()->request->getParam('oldpwd');
			$password = Yii::app()->request->getParam('password');
			$password2 = Yii::app()->request->getParam('password2');
			if(self::hash_password($oldpwd) != $admin->password)
			{
				$this->alert_error('旧密码错误');
			}
			if(!$password)
			{
				$this->alert_error('密码不能为空');
			}
			if($password != $password2)
			{
				$this->alert_error('2次输入的新密码不一致');
			}
			$admin->password = self::hash_password($password);
			$admin->save();
			$this->alert_ok();
		}
		else 
		{
			$this->display('editmypwd');
		}
	}
}